package com.jdjw.modules.bushouseapply.controller;


import com.google.common.collect.Lists;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApplyCommit;
import com.jdjw.modules.bushouseapply.entity.BusHouseApply;
import com.jdjw.modules.bushouseapply.entity.BusHouseApplySave;
import com.jdjw.modules.bushouseapply.entity.BusHouseApplyVo;
import com.jdjw.modules.bushouseapply.service.BusHouseApplyService;
import com.jdjw.modules.bushouseapplyperson.entity.BusHouseApplyPerson;
import com.jdjw.modules.bushouseapplyperson.service.BusHouseApplyPersonService;
import com.jdjw.modules.wfactivity.entity.WfActivity;
import com.jdjw.modules.wfactivity.service.WfActivityService;
import com.jdjw.modules.wftask.entity.WfTask;
import com.jdjw.modules.wftask.service.WfTaskService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 困难户危旧房救助 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-07-22
 */
@RestController
@RequestMapping("/bushouseapply")
public class BusHouseApplyController {

    @Autowired
    private BusHouseApplyService busHouseApplyService;
    
    @Autowired
    private BusHouseApplyPersonService busHouseApplyPersonService;

    @Autowired
    private WfActivityService wfActivityService;

    @Autowired
    private WfTaskService wfTaskService;



    @ApiOperation("困难户危旧房救助-查询")
    @PostMapping("queryAll")
    public ResponseResult queryAll(BusHouseApplyVo busHouseApplyVo){
        return  busHouseApplyService.queryAll(busHouseApplyVo);
    }


    @ApiOperation("困难户危旧房救助-添加")
    @PostMapping("save")
    public ResponseResult save(@RequestBody BusHouseApplySave busHouseApplySave){
        ResponseResult rrt=new ResponseResult();
        try {
            BusHouseApply busHouseApply=new BusHouseApply();
            BeanUtils.copyProperties(busHouseApplySave,busHouseApply);
            busHouseApply.setOrgCode(busHouseApplySave.getDeptId());
            busHouseApply.setCreateDate(new Date());
            busHouseApply.setPassFlag("0");
            busHouseApplyService.save(busHouseApply);
            if(busHouseApplySave.getPersonList()!=null ){
                for (BusHouseApplyPerson busHouseApplyPerson : busHouseApplySave.getPersonList()) {
                    busHouseApplyPerson.setHouseApplyId(busHouseApply.getId());
                    busHouseApplyPersonService.save(busHouseApplyPerson);
                }
            }
            wfActivityService.createWorkFlow("KNHWXJZ", busHouseApplySave.getDeptId(),busHouseApply.getId(),busHouseApplySave.getCreateBy(),busHouseApplySave.getUserName());
            rrt.setMsg("保存成功");
            rrt.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setMsg("保存失败");
            rrt.setCode(500);
        }

        return rrt;
    }

    @ApiOperation("困难户危旧房救助-编辑")
    @PostMapping("edit")
    public ResponseResult edit(@RequestBody String sqlEdit) throws JSONException {
        JSONObject json=new JSONObject(sqlEdit);
         return busHouseApplyService.sqlEdit(json.getString("sqlEdit"));
    }


    @ApiOperation("困难户危旧房救助-删除")
    @PostMapping("delete")
    public ResponseResult delete(String id){
        ResponseResult rrt=new ResponseResult();
        boolean b = busHouseApplyService.removeById(id);
        if(b){
            rrt.setMsg("删除成功");
            rrt.setCode(200);
        }else{
            rrt.setMsg("删除失败");
            rrt.setCode(500);
        }
        return rrt;
    }

    @ApiOperation("困难户危旧房救助-查看")
    @PostMapping("{id}/preview")
    public  ResponseResult preview(@PathVariable("id") String id){
        ResponseResult rrt=new ResponseResult();
        try {
            BusHouseApply apply  = busHouseApplyService.getById(id);
            Map<String,Object> map=new HashMap<>();
            map.put("apply_id",apply.getId());
            List<WfActivity> activityList = (List<WfActivity>) wfActivityService.listByMap(map);
            List<WfTask> taskList = Lists.newArrayList();
            if(activityList!= null && activityList.size()>0){
                taskList = wfTaskService.queryByactivityId(activityList.get(0).getId());
                apply.setTaskList(taskList);
            }
            Map<String,Object> map2=new HashMap<>();
            map2.put("house_apply_id", apply.getId());
            List<BusHouseApplyPerson> personList = (List<BusHouseApplyPerson>) busHouseApplyPersonService.listByMap(map2);

            String nodeCode = wfActivityService.findNodeCode(apply.getId());
            apply.setNodeCode(nodeCode);//节点标识
            apply.setPersonList(personList);//流程详情
            rrt.setMsg("查看成功");
            rrt.setCode(200);
            rrt.setData(apply);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setMsg("查看错误");
            rrt.setCode(500);
            rrt.setData(null);
        }
        return rrt;
    }

    @ApiOperation("一般性补助救助-提交")
    @PostMapping("commit")
    public ResponseResult commit(@RequestBody BusGeneralApplyCommit busGeneralApplyCommit) {
        ResponseResult rrt = new ResponseResult();

        BusHouseApply byId = busHouseApplyService.getById(busGeneralApplyCommit.getId());
        String deptId2 = busGeneralApplyCommit.getDeptId();
        String nodeCode = wfActivityService.findNodeCode(byId.getId());
        try {
            WfTask wfTask = busGeneralApplyCommit.getWfTask();
            if (wfTask == null) {
                wfTask = new WfTask();
            }
            wfTask.setAttIds(busGeneralApplyCommit.getWfTask().getAttIds());
            String dualOpinion = StringUtils.isBlank(wfTask.getDualOpinion()) ? "01" : wfTask.getDualOpinion();
            BusHouseApply busApply1 = busHouseApplyService.getById(byId.getId());
            if ("01".equals(dualOpinion)) {  // 同意
                // 判断是否是特殊节点
//                String[] userIdS = new String[]{busApply1.getCreateBy()};
                if("XZYS".equals(nodeCode)){
                    busApply1.setPassFlag("2");
                    busHouseApplyService.updateById(busApply1);
                    wfActivityService.sendNextNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
                } else {
                    wfActivityService.sendNextNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
                }
            } else if ("02".equals(dualOpinion)) { // 不同意
                busApply1.setPassFlag("1");
                busHouseApplyService.updateById(busApply1);
                wfActivityService.sendAppointNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, "GD", wfTask);
            } else if ("04".equals(dualOpinion)) { // 退回
                wfActivityService.sendBack(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
            }
            rrt.setCode(200);
            rrt.setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("提交失败");
        }
        return rrt;
    }

}

